package ai.zixing.mashibing.basic_class.class06;

public class Code03_SmallerEqualBigger {

    public static Node listPartition1(Node head, int pivot) {
        if (head == null) {
            return head;
        }
        Node cur = head;
        // 统计节点数量
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        // 存入数组
        Node[] nodeArr = new Node[size];
        cur = head;
        for (int i = 0; i < size; i++) {
            nodeArr[i] = cur;
            cur = cur.next;
        }
        // 数组内分区
        arrPartition(nodeArr, pivot);
        // 重新串联链表
        for (int i = 1; i < nodeArr.length; i++) {
            nodeArr[i - 1].next = nodeArr[i];
        }
        nodeArr[size - 1].next = null;
        return nodeArr[0];
    }

    public static void arrPartition(Node[] nodeArr, int pivot) {
        int L = 0;
        int R = nodeArr.length;
        int index = 0;
        while (index < R) {
            if (nodeArr[index].value < pivot) {
                swap(nodeArr, L++, index++);
            } else if (nodeArr[index].value == pivot) {
                index++;
            } else {
                swap(nodeArr, --R, index);
            }
        }

    }

    public static void swap(Node[] nodeArr, int i, int j) {
        Node temp = nodeArr[i];
        nodeArr[i] = nodeArr[j];
        nodeArr[j] = temp;
    }

    public static Node listPartition2(Node head, int pivot) {
        Node sH = null;
        Node sT = null;
        Node eH = null;
        Node eT = null;
        Node bH = null;
        Node bT = null;
        Node next = null;
        while (head != null) {
            next = head.next;
            // 每次只取一个节点
            head.next = null;
            if (head.value < pivot) {
                if (sH == null) {
                    sH = head;
                    sT = head;
                } else {
                    sT.next = head;
                    sT = head;
                }
            } else if (head.value == pivot) {
                if (eH == null) {
                    eH = head;
                    eT = head;
                } else {
                    eT.next = head;
                    eT = head;
                }
            } else {
                if (bH == null) {
                    bH = head;
                    bT = head;
                } else {
                    bT.next = head;
                    bT = head;
                }
            }
            head = next;
        }
        // 将三部分串起来
        // 有小于区
        if (sT != null) {
            sT.next = eH;
            // 下一步，谁去连大于区域的头，谁就变成eT
            eT = eT == null ? sT : eT;
        }
        // 存在等于区
        if (eT != null) {
            eT.next = bH;
        }
        // 存在大于区
        return sH != null ? sH : (eH != null ? eH : bH);
    }

    public static void printLinkedList(Node node) {
        System.out.print("Linked List: ");
        while (node != null) {
            System.out.print(node.value + " ");
            node = node.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head1 = new Node(7);
        head1.next = new Node(9);
        head1.next.next = new Node(1);
        head1.next.next.next = new Node(8);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(2);
        head1.next.next.next.next.next.next = new Node(5);
        printLinkedList(head1);
         head1 = listPartition1(head1, 4);
//        head1 = listPartition2(head1, 4);
        printLinkedList(head1);

    }
}
